package com.xx.service.impl;

import com.xx.dao.FindUserMapper;
import com.xx.domain.Role;
import com.xx.domain.User;
import com.xx.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @Author : 徐闯 ssm_admin
 * @Date : 2021-05-15 22:05:14
 * @Description : 认证的service
 **/
@Service("loginService")
public class LoginServiceImpl implements LoginService {
    @Autowired(required = true)
    private FindUserMapper findUserMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = findUserMapper.findOne(username);
        // 将用户对象的信息 封装到UserDetails中
        /*org.springframework.security.core.userdetails.User u
                = new org.springframework.security.core.userdetails.User(user.getUsername(),"{noop}"+user.getPassword(),getRoleList(user.getRoleList()));*/
        /*org.springframework.security.core.userdetails.User u
                = new org.springframework.security.core.userdetails.User(user.getUsername(),"{noop}"+user.getPassword(),user.getStatus()==1?true:false,true,true,true,getRoleList(user.getRoleList()));*/
        org.springframework.security.core.userdetails.User u
                = new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),user.getStatus()==1?true:false,true,true,true,getRoleList(user.getRoleList()));
        return u;
    }
    // 获取权限集合
    public List<GrantedAuthority> getRoleList(List<Role> roleList){
        List<GrantedAuthority> list = new ArrayList();
        for(Role role : roleList){
            list.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName().toUpperCase()));
        }
        return list;
    }
}
